﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// EventAjax 的摘要说明
    /// </summary>
    public class EventAjax : IHttpHandler
    {
        EventController eventController = new EventController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetTaskCondition"://获取可作为任务条件的模板
                        GetTaskCondition(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetTaskCondition(HttpContext context)
        {
            string json = JsonHelper.ListToJson<Event>(eventController.GetList(" IsTaskCondition=1"));
            context.Response.Write(json);
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            EventChanceController eventChanceController = new EventChanceController();
            if (eventChanceController.DeleteByEventID(id))
            {
                EventMouldController controller = new EventMouldController();
                bool isSuccess = controller.DeleteByEventID(id);
                if (isSuccess)
                    context.Response.Write(eventController.Delete(id));
                return;
            }
            context.Response.Write(false);
        }

        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            int changeNum = Convert.ToInt32(context.Request["changeNum"].ToString());
            string typeName = context.Request["typeName"];
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());

            Event model = new Event()
            {
                ChangeNum = changeNum,
                Name = name,
                TypeName = typeName,
                IsTaskCondition = isTaskCondition
            };

            context.Response.Write(eventController.Add(model));
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string name = context.Request["name"];
            int changeNum = Convert.ToInt32(context.Request["changeNum"].ToString());
            string typeName = context.Request["typeName"];
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());

            Event model = new Event()
            {
                ChangeNum = changeNum,
                ID = id,
                Name = name,
                TypeName = typeName,
                IsTaskCondition = isTaskCondition
            };
            context.Response.Write(eventController.Update(model));
        }

        private void GetList(HttpContext context)
        {
            string json = JsonHelper.ListToJson<Event>(eventController.GetList());
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}